package com.byron.pss.web.action;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import com.byron.pss.dao.ILostFoundDAO;
import com.byron.pss.dao.impl.LostFoundDAOImpl;
import com.byron.pss.domain.LostFoundStr;
import com.byron.pss.util.Transform;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private ILostFoundDAO dao = new LostFoundDAOImpl();
	private String fileName = "lostFound.xls";// 被下载文件的名称,用于在XML中设置保存的文件名称

	@Override
	public String execute() throws Exception {
		System.out.println("api....");
		return SUCCESS;
	}

	// 文件下载
	// TODO :乱码
	public InputStream getTargetFile() throws Exception {
		String title = "编号\t物品名称\t描述\t遗失位置\t遗失时间\t数量\t其它信息\r\n";
		// 查询所有的商品信息
		List<LostFoundStr> list = Transform.list0jsp(dao.list());
		StringBuilder sb = new StringBuilder();
		sb.append(title);
		for (LostFoundStr lfstr : list) {
			sb.append(lfstr.getId()).append("\t");
			sb.append(lfstr.getName()).append("\t");
			sb.append(lfstr.getDescribe()).append("\t");
			sb.append(lfstr.getLocation()).append("\t");
			sb.append(lfstr.getLostTime()).append("\t");
			sb.append(lfstr.getNumber()).append("\t");
			sb.append(lfstr.getOther()).append("\r\n");
		}
		InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
		return new BufferedInputStream(is);
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	// 向外提供属性
	public String getFileName() {
		return fileName;
	}
}
